package com.sj.client;

import com.sj.pojo.FirstProtocol;
import com.sj.pojo.MsgTypeEnum;
import com.sj.pojo.Protocol;
import com.sj.utils.TransferUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2022-07-07
 * Time: 下午 12:02
 * Description: 传输客户端
 *
 * @author xiaoqi
 * @since 2022-07-07
 */
public class TransferClient {

    public static void init() {
        try {
            Selector selector = Selector.open();
            SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(7005));
            socketChannel.configureBlocking(false);
            socketChannel.register(selector, SelectionKey.OP_READ);

            new Thread(new ClientThread(selector)).start();

            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()) {
                String msg = sc.nextLine();
                Protocol protocol = new FirstProtocol();
                Protocol res = protocol.getProtocol(MsgTypeEnum.REQUEST, msg);
                TransferUtils.send(res, socketChannel);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}